import Vue from 'vue'
import Vuerouter from 'vue-router'

//引入组件
import index from "../components/index"
import login from "../components/Login"
import Welcome from "../components/Welcome"
import Home from "../components/Home"
import user from "../components/admin/UserList"
import UserSave from "../components/admin/UserSave"
//  显示声明router路由  告诉vue使用 vueRouter
Vue.use(Vuerouter);

const routes = [
    {
        path:'/',
        name:'登录页',
        redirect:'/login',  //转发到login路由中去
    },
    {
        path:"/login",
        component:login   //跳转显示login页面内容
    },
    {
        path:"/Home",
        component:Home,
        redirect:"/Welcome",
        children:[
            {
                path:"/Welcome",
                component:Welcome
            },
            {
                path:"/user",
                component:user
            },
            {
                path:"/UserSave",
                component:UserSave
            }
        ]
    },
]

const router = new Vuerouter({
    mode:'history',   //让访问路径上的#消失
    routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // - to：路由将要跳转的路径信息
    // - from：路径跳转前的路径信息
    // - next：路由的控制参数
    // - next() 跳入下一个页面
    // - next(’/path’) 改变路由的跳转方向，使其跳到另一个路由
    // - next(false) 返回原来的页面
    // - next((vm)=>{}) 仅在 beforeRouteEnter 中可用，vm 是组件实例
    console.log("准备进入个人信息页");
    if (to.path == '/login') return next();// 访问路径为登录
    // 获取flag
    let flagStr = window.sessionStorage.getItem("user");// session取值
    if (flagStr==null) return next('/login');// 没登录去登录
    next();
})

export default router// 暴露出去
